﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using DataAccessLayer.DAL;


namespace BestBuy.UserViews
{
    public partial class Feedback : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AuctionTransaction aucTransactions = null;
            User user = UserDAL.GetUserByUsername(HttpContext.Current.User.Identity.Name);
            if (user == null)
            {
                Response.Redirect("~/Account/Login.aspx");
            }
            if (Request.QueryString["AuctionID"] != null)
            {
                int id = int.Parse(Request.QueryString["AuctionID"]);
                aucTransactions = UserDAL.GetFeedbackByAuctionID(id);
            }
            if (!IsPostBack)
            {
                LoadFeedback(user, aucTransactions);
            }
            
        }

        private void LoadFeedback(User user, AuctionTransaction auc)
        {
            if (auc == null)
            {
                FeedbackMessage.Show("Don't have feedback", UserControls.MessagePanel.MessageType.Error);
                return;
            }
            if (auc.SellerID == user.ID || auc.Auction.CreateByUserID == user.ID)
            {
                CommentPanel.Visible = true;
            }
            AuctionID.Value = auc.AuctionID.ToString();
            gvFeedback.DataSource = auc.TransactionFeedbacks;
            gvFeedback.DataBind();
        }

        protected void gvFeedback_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //TransactionFeedback feedback = (TransactionFeedback)DataBinder.Eval(e.Row.DataItem, "TransactionFeedback");
                DataAccessLayer.User fromuser = (DataAccessLayer.User)DataBinder.Eval(e.Row.DataItem, "User");
                DataAccessLayer.User touser = (DataAccessLayer.User)DataBinder.Eval(e.Row.DataItem, "User1");

                if (fromuser != null && touser != null)
                {
                    Label from = e.Row.FindControl("lbFromUser") as Label;
                    Label to = e.Row.FindControl("lbToUser") as Label;
                    Label rating = e.Row.FindControl("lbRating") as Label;
                    Label comment = e.Row.FindControl("lbComment") as Label;
                    if (from != null && to != null && rating != null && comment != null)
                    {
                        from.Text = fromuser.Username;
                        to.Text = touser.Username;
                        rating.Text = ((int)DataBinder.Eval(e.Row.DataItem, "RatingValue")).ToString();
                        comment.Text = (string)DataBinder.Eval(e.Row.DataItem, "Comment");
                    }
                }
            }
        }

        protected void gvFeedback_DataBound(object sender, EventArgs e)
        {
        }

        protected void btnPostComment_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                if (Request.QueryString["AuctionID"] == null || txtComment.Text == "")
                    return;

                int id = 0;
                int.TryParse(Request.QueryString["AuctionID"], out id);
                AuctionTransaction auc = UserDAL.GetFeedbackByAuctionID(id);
                int userID = UserDAL.GetUserIDByUsername(HttpContext.Current.User.Identity.Name);
                if (auc != null)
                {
                    TransactionFeedback feedback = new TransactionFeedback();
                    feedback.AuctionID = auc.AuctionID;
                    feedback.CreateByUserID = userID;
                    feedback.RatingValue = int.Parse(cbRating.SelectedValue.ToString());
                    feedback.Comment = txtComment.Text;
                    feedback.Status = 0;
                    if (userID == auc.SellerID)
                    {
                        feedback.FeedbackForUserID = auc.Auction.CreateByUserID;
                    }
                    else if (userID == auc.Auction.CreateByUserID)
                    {
                        feedback.FeedbackForUserID = auc.SellerID;
                    }

                    UserDAL.AddFeedback(feedback);
                    Response.Redirect(Request.RawUrl);
                }
            }
        }
    }
}